package cn.ylz.ylmall.aftersale.service.impl;

import cn.ylz.ylmall.aftersale.dao.persist.repository.AfterSaleRepository;
import cn.ylz.ylmall.aftersale.pojo.entity.AfterSale;
import cn.ylz.ylmall.aftersale.pojo.request.AfterSaleReq;
import cn.ylz.ylmall.aftersale.pojo.vo.AfterSaleVO;
import cn.ylz.ylmall.aftersale.service.AfterSaleService;
import cn.ylz.ylmall.common.ex.ServiceException;
import cn.ylz.ylmall.common.web.ServiceCode;
import cn.ylz.ylmall.order.dao.persist.repository.OrderDetailRepository;
import cn.ylz.ylmall.order.pojo.entity.OrderDetail;
import cn.ylz.ylmall.order.pojo.vo.OrderDetailVO;
import cn.ylz.ylmall.order.pojo.vo.OrderItemImgVO;
import cn.ylz.ylmall.order.pojo.vo.OrderItemVO;
import cn.ylz.ylmall.order.service.OrderDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author pc
 */
@Service
public class AfterSaleServiceImpl implements AfterSaleService {

    @Autowired
    private OrderDetailRepository orderDetailRepository;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private AfterSaleRepository afterSaleRepository;

    /**
     * 添加售后
     * @param afterSaleReq
     * @param userId
     * @return
     */
    @Override
    public int insertAfterSale(AfterSaleReq afterSaleReq, Long userId) {

        OrderDetail orderDetail = orderDetailRepository.selectOrderVO(afterSaleReq.getOrderNo());

        if(!orderDetail.getUserId().equals(userId)){
            throw new ServiceException(ServiceCode.ERROR_ORDER_MISMATCH,"订单不匹配");
        }

        AfterSale afterSale = new AfterSale();

        afterSale.setUserId(userId);

        BeanUtils.copyProperties(afterSaleReq,afterSale);

        afterSale.setCreatedTime(new Date());

        return afterSaleRepository.insertAfterSale(afterSale);
    }

    /**
     * 前台查询售后列表
     * @param userId
     * @return
     */
    @Override
    public List<AfterSaleVO> selectAfterSale(Long userId) {

        List<AfterSale> afterSaleList = afterSaleRepository.selectAfterSaleList(userId);

        List<AfterSaleVO> afterSaleVOList = new ArrayList<>();
        for (int i = 0; i < afterSaleList.size(); i++) {
            AfterSale afterSale = afterSaleList.get(i);
            afterSaleVOList.add(afterSaleToAfterSaleVO(afterSale));
        }

        return afterSaleVOList;
    }

    /**
     * 后台分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo selectAfterSalePage(Integer pageNum, Integer pageSize) {

        List<AfterSale> afterSaleList = afterSaleRepository.selectAll();
        List<AfterSaleVO> afterSaleVOList = new ArrayList<>();
        for (int i = 0; i < afterSaleList.size(); i++) {
            AfterSale afterSale = afterSaleList.get(i);
            afterSaleVOList.add(afterSaleToAfterSaleVO(afterSale));
        }
        PageHelper.startPage(pageNum,pageSize);
        PageInfo pageInfo = new PageInfo(afterSaleVOList);
        pageInfo.setList(afterSaleVOList);
        return pageInfo;
    }

    @Override
    public int delectByOrderNo(String orderNo) {
        return afterSaleRepository.delectByOrderNo(orderNo);
    }


    private AfterSaleVO afterSaleToAfterSaleVO(AfterSale afterSale){
        AfterSaleVO afterSaleVO = new AfterSaleVO();

        BeanUtils.copyProperties(afterSale,afterSaleVO);

        OrderDetailVO orderDetailVO = orderDetailService.selectOrder(afterSaleVO.getOrderNo(),afterSale.getUserId());

        OrderItemImgVO orderItemVO = orderDetailVO.getOrderItemImgVO();

        afterSaleVO.setProductImg(orderItemVO.getProductImgs());
        afterSaleVO.setProductName(orderItemVO.getProductName());
        afterSaleVO.setQuantity(orderItemVO.getQuantity());
        afterSaleVO.setTotalPrice(orderItemVO.getTotalPrice());
        afterSaleVO.setUnitPrice(orderItemVO.getUnitPrice());

        return afterSaleVO;
    }
}
